// index.js
import {
  debounce,
  formatDate
} from "../../utils/util"

import {
  // 查询商品
  shopIndex,
  shopIndexRecommend,
  // 添加购物车
  addCart,
  //获取购物车
  getCart,
  // 获取每日通知
  getNotices
} from '../../api/index'

Page({
  data: {
    // 商品数组 新品
    newProductArr: [],
    // 商品数据 推荐
    recommendArr: [],
    // 类别选择下标
    typeNum: 0,
    // 积分选择区间
    integral1: '',
    integral2: '',
    // 搜索条件
    searchValue: '',
    // 下拉刷新
    refBob1: false,
    refBob2: false,
    // 新品专区
    typeTitle: '新品专区',
    active: 0,
    // 每日通知
    notShow: false,
    // 通知
    noticesArr: []
  },
  onShow() {
    console.log('show');
    this.getShopGoods();
  },
  onLoad() {
    this.getNotice()
  },
  // 获取每日通知
  async getNotice() {
    try {
      const dataObj = await getNotices()
      console.log("获取每日通知", dataObj);
      if (dataObj.code == 200) {
        if (dataObj.data.notice_list.length == 0) {
          this.setData({
            noticesArr: dataObj.data.notice_list
          })
        } else {
          dataObj.data.notice_list.forEach(item => {
            item.create_time = formatDate(new Date(item.create_time).getTime())
          })
          this.setData({
            noticesArr: dataObj.data.notice_list,
            notShow: true
          })
        }
      }
    } catch (error) {
      console.log("获取每日通知错误：", error);
    }

  },
  // 关闭每日弹窗
  onNotClose() {
    this.setData({
      notShow: false
    })
  },
  // 获取商品信息
  async getShopGoods() {
    const {
      integral1,
      integral2,
      searchValue,
      active
    } = this.data
    console.log("传递的参数是", searchValue);
    const cartObj = await getCart()
    console.log("购物车：", cartObj);
    if (active == '0') {
      const dataObj = await shopIndex({
        page_num: 1,
        page_size: 10000,
        start_price: integral1,
        end_price: integral2,
        name: searchValue,
        status: '1'
      })
      console.log("订单查询结果：", dataObj);
      if (dataObj.code == 200) {
        // dataObj.data.forEach(item => {
        //   item.create_time = formatDate(item.create_time)
        // })
        if (cartObj.code == 200) {
          dataObj.data.data_list.forEach(item => {
            let obj = cartObj.data.find(items => items.goods_id == item.id)
            item.cartNum = obj ? obj.goods_count : 0
          })
        }
        this.setData({
          newProductArr: dataObj.data.data_list
        })
      }
    } else {
      const dataObj = await shopIndexRecommend({
        page_num: 1,
        page_size: 10000,
        start_price: integral1,
        end_price: integral2,
        name: searchValue,
        type: active
      })
      console.log("订单查询结果：", dataObj);
      if (dataObj.code == 200) {
        // dataObj.data.forEach(item => {
        //   item.create_time = formatDate(item.create_time)
        // })
        if (cartObj.code == 200) {
          dataObj.data.forEach(item => {
            let obj = cartObj.data.find(items => items.goods_id == item.goods.id)
            item.goods.cartNum = obj ? obj.goods_count : 0
          })
        }
        this.setData({
          recommendArr: dataObj.data
        })
      }
    }
  },

  // 跳转商品详情
  toDetails(e) {
    const obj = e.currentTarget.dataset.obj
    console.log("商品数据:", obj);
    wx.navigateTo({
      url: '/pages/index/productDetails/productDetails?obj=' + JSON.stringify(obj),
    })
  },
  // 下拉刷新
  // 新品下拉开始
  refStart1: debounce(function () {
    console.log("下拉");
    setTimeout(() => {
      this.setData({
        refBob1: false
      })
    }, 2000)
  }, 500),
  // 新品下拉完成
  refSuccess1() {
    this.getShopGoods();
    console.log("触发1");
  },
  // 推荐下拉开始
  refStart2: debounce(function () {
    console.log("下拉");
    setTimeout(() => {
      this.setData({
        refBob2: false
      })
    }, 2000)
  }, 500),
  // 推荐下拉结束
  refSuccess2() {
    this.getShopGoods();
    console.log("触发2");
  },

  // 搜索商品
  onClickSearch() {
    this.getShopGoods();
  },
  // 顶部标签切换
  onChangeTab(e) {
    console.log(e.detail);
    this.setData({
      typeTitle: e.detail.title,
      active: e.detail.index
    })
    this.getShopGoods();
  },
  // 添加购物车
  async addCartPage(e) {
    try {
      const goods_id = e.currentTarget.dataset.id
      const goods_count = e.currentTarget.dataset.count + 1
      console.log(goods_id, goods_count);
      const dataObj = await addCart({
        goods_id,
        goods_count
      })
      console.log("添加购物车:", dataObj);
      if (dataObj.code == 200) {
        this.getShopGoods()
      }
    } catch (error) {
      console.log("添加购物车错误", error);
    }
  }

})